using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Deepcode.Flux.Core.Data;
using Deepcode.Flux.Core.Systems.Workflow.Bases;
using Deepcode.Flux.Core.Systems.Workflow;
using Deepcode.Flux.Core.Services.State;
using Deepcode.Flux.Core.Systems.Audit.Quick;

public partial class Private_Config_Workflow_ui_Processes : Deepcode.Flux.Core.UI.AdministrationModulePage
{
	#region Permission Code Override
	public override string RequiredPermissionCode { get { return "FluxWorkflowEdit"; } }
	#endregion
	#region ViewState storage
	/// <summary>
	/// Stores the ID of the handler which this form
	/// is adding a new definition to.
	/// </summary>
	public string HandlerID
	{
		get
		{
			object o = ViewState["HandlerID"];
			if( o == null ) return String.Empty;
			return (string) o;
		}
		set
		{
			ViewState["HandlerID"] = value;
		}
	}
	/// <summary>
	/// Stores the ID of the workflow definition being edited
	/// </summary>
	public int DefinitionID
	{
		get
		{
			object o = ViewState["DefinitionID"];
			if (o == null) return -1;
			return (int)o;
		}
		set
		{
			ViewState["DefinitionID"] = value;
		}
	}
	#endregion
	#region Page Load event
	/// <summary>
	/// Page Load event
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!Page.IsPostBack)
		{
			// Get URL Values
			HandlerID = GetAttribute("HandlerID", "");
			DefinitionID = GetAttribute("DefinitionID", -1);

			// Load handler etc for display info
			Handler h = new Handlers().GetHandlerByID( HandlerID );
			if( h == null ) GoBack();

			Definition d = new Definitions().GetByID( DefinitionID );
			if( d == null ) GoBack();

			// Display infos
			labHandler.Text = h.Name;
			labDefinition.Text = d.DefName;

			// Bind data
			BindDataGrid();
		}
	}
	#endregion
	#region Initialisation
	/// <summary>
	/// OnInit - hooks up the data paging handler, 
	/// rpp change handler and item command handlers
	/// </summary>
	/// <param name="e"></param>
	protected override void OnInit(EventArgs e)
	{
		base.OnInit(e);
		datagrid.ItemCommand += new DataGridCommandEventHandler(GridCommand);
	}
	#endregion
	#region Data binding and paging events
	/// <summary>
	/// Data binding
	/// </summary>
	private void BindDataGrid()
	{
		// Add javascript call to delete buttons
		datagrid.BindJavascript("btnDelete", "onclick", "return ConfirmDelete();");
	
		// Get and bind the data
		Processes procs = new Processes();
		datagrid.DataSource = procs.GetProcessByDefinitionID( this.DefinitionID );
		datagrid.DataBind();
	}
	#endregion
	#region User interactions
	/// <summary>
	/// Command received from the data grid
	/// </summary>
	/// <param name="source"></param>
	/// <param name="e"></param>
	void GridCommand(object source, DataGridCommandEventArgs e)
	{
		if( e.Item.ItemType == ListItemType.Footer ||
			e.Item.ItemType == ListItemType.Header ||
			e.Item.ItemType == ListItemType.Pager ||
			e.Item.ItemType == ListItemType.Separator ) return;

		// Get the key of the row being actioned 
		int rowKey = (int) datagrid.DataKeys[e.Item.ItemIndex];

		// PROCESS COMMANDS
		if( e.CommandName == "ItemDelete" )
		{
			Processes ps = new Processes();
			Process p = ps.GetProcessByID( rowKey );

			ps.DeleteByID( rowKey );

			QuickAudit.RaiseImportant(
				new AuditUser(),
				"Flux", "Workflow Manager",
				"deleted workflow process ({0}) in ({1})",
				p.ProcessName, p.ParentDefinition.DefName );
			
			this.BindDataGrid();
			return;
		}
		if( e.CommandName == "ItemEdit" )
		{
			GoEditProcess( rowKey );
			return;
		}
	}
	/// <summary>
	/// User clicked the add new button
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void btnAddNew_Click(object sender, EventArgs e)
	{
		GoAddProcess();
	}
	/// <summary>
	/// User clicked the back button
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void btnBack_Click(object sender, EventArgs e)
	{
		GoBack();
	}
	#endregion
	#region Navigation
	/// <summary>
	/// Return the user BACK to the handlers list
	/// </summary>
	protected void GoBack()
	{
		string url = String.Format(
			"ui_Definitions.aspx?HandlerID={0}",
			this.HandlerID);

		Response.Redirect(url, true);
	}
	/// <summary>
	/// Send user forward to edit the selected process
	/// </summary>
	/// <param name="DefinitionID"></param>
	protected void GoEditProcess( int ProcessID )
	{
		string url = String.Format(
			"ui_ProcessForm.aspx?HandlerID={0}&DefinitionID={1}&ProcessID={2}",
			this.HandlerID,
			this.DefinitionID,
			ProcessID);
		
		Response.Redirect(url, true);
	}
	/// <summary>
	/// Send user forward to creating a process
	/// </summary>
	protected void GoAddProcess()
	{
		string url = String.Format(
			"ui_newprocessform.aspx?HandlerID={0}&DefinitionID={1}",
			this.HandlerID,
			this.DefinitionID);

		Response.Redirect(url, true);
	}
	#endregion
}
